/*
 * Created on Mar 14, 2004
 */
package com.scalar.chiptrack.operations.packingslip.actions;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.chiptrack.operations.packingslip.CustomerShippingLineItems;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;

/**
 * @author Ravikanth
**/
public class ConfirmPackingSlipAction extends ChipTrackAction{
	
	public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
	{
		HttpSession session = request.getSession();
		String returnVal 	= "failure";
		String action 		= request.getParameter( "action" );
		boolean dropShip	= false;
		int customerShippingId  = -1;
		HashMap errors			= new HashMap();
		HashMap info 			= new HashMap();
		HashMap lineItemMap 	= null;
		HashMap finalInventoryMap = null;
		PackingSlipForm packingSlipForm = ( PackingSlipForm ) form;
	
		User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
		String currentUser = user.getUserId();
        try
		{
            if ( Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED.equalsIgnoreCase( NewPackingSlipDAO.getPackingSlipStatus( null, packingSlipForm.getCustomerShippingId() ) ) )
            {
                errors.put ( "PackingSlip (" + packingSlipForm.getLabel() + ") is already confirmed", null );
            }
            else
            {
                if(request.getParameter("autoconfirm") != null && "true".equalsIgnoreCase(request.getParameter("autoconfirm"))) {
                    if(NewPackingSlipDAO.isPackingSlipReadyForConfirm(null, packingSlipForm.getCustomerShippingId())) {
                        packingSlipForm = NewPackingSlipDAO.loadPackingSlipById ( packingSlipForm.getCustomerShippingId() );
                    } else {
                        errors.put("Packingslip is not ready for confirm", null);
                        return (mapping.findForward ("packingSlipSearch"));
                    }
                }
                if( session.getAttribute( SessionKeys.FINAL_INVENTORY_MAP ) != null)
                {
                    finalInventoryMap = (HashMap) session.getAttribute ( SessionKeys.FINAL_INVENTORY_MAP );
                    HashMap inventoryDataMapDB = NewPackingSlipDAO.getFinalInventoryMapByLineItemId( NewPackingSlipDAO.loadLotNumberDataByCustomerShippingId ( packingSlipForm.getCustomerShippingId() ) );
                    //HashMap inventoryDataMapDB = NewPackingSlipDAO.loadLotNumberAndLineItemDataByCSID ( packingSlipForm.getCustomerShippingId() );
                    loadNewFinalInventoryDataListMap( finalInventoryMap, inventoryDataMapDB );
                }
                else
                {
                    finalInventoryMap = NewPackingSlipDAO.loadLotNumberAndLineItemDataByCSID ( packingSlipForm.getCustomerShippingId() );
                }
                NewPackingSlipDAO.confirmPackingSlip ( packingSlipForm, finalInventoryMap, currentUser );
                packingSlipForm.setStatus( Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED );
                info.put ( "PackingSlip (" + packingSlipForm.getLabel() + ") has been confirmed(Invoice #: " + packingSlipForm.getInvNo() + ")", null );
                try
                {
                    boolean mailSuccess;
                    if ( ConfigManager.isSendShipmentNotificationMail() )
                    {
                        if( packingSlipForm.getCustomerNumber().equalsIgnoreCase( packingSlipForm.getVendorName() ) ) //SHIPPING FROM THE CUSTOMER CONSIGNMENT WAAREHOUSE TO THE CUSTOMER
                        {
                            if ( ConfigManager.isSendShipmentNotificationWhenPulledFromConsignmentWareHouse() )
                            {
                                mailSuccess = ShippingAdviseAction.sendShipmentNotificationMail( mapping, request, response, getShipAlertMessageBody( request.getHeader( "HOST" ), packingSlipForm ), packingSlipForm.getCustomerShippingId(), packingSlipForm.getLabel() );
                                if ( mailSuccess )
                                {
                                    info.put( "Email notification sent successfully", null );
                                }
                                else
                                {
                                    errors.put ( "Could not send email notification", null );
                                }
                            }
                        }
                        else
                        {
                            mailSuccess = ShippingAdviseAction.sendShipmentNotificationMail( mapping, request, response, getShipAlertMessageBody( request.getHeader( "HOST" ), packingSlipForm ), packingSlipForm.getCustomerShippingId(), packingSlipForm.getLabel() );
                            if ( mailSuccess )
                            {
                                info.put( "Email notification sent successfully", null );
                            }
                            else
                            {
                                errors.put ( "Could not send email notification", null );
                            }
                        }
                    }
                }
                catch ( Exception e )
                {
                    e.printStackTrace();
                    errors.put ( "Could not send email notification", null );
                }
            }
            request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        }
		catch ( ScalarException se )
		{
			se.printStackTrace();
			errors.put ( "Internal Server Error", "Could not confirm packingslip (" + packingSlipForm.getLabel() + ")" );
			errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
			session.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not confirm packingslip (" + packingSlipForm.getLabel() + ")" );
			session.setAttribute( SessionKeys.ERROR_REPORT, se );
			request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		}
        if(request.getParameter("autoconfirm") != null && "true".equalsIgnoreCase(request.getParameter("autoconfirm"))) {
            return (mapping.findForward ("packingSlipSearch"));
        }
		return (mapping.findForward ("packingSlipMenu"));
	}

    private HashMap loadNewFinalInventoryDataListMap ( HashMap existingFinalInventoryDataListMap , HashMap dbFinalInventoryDataListMap )
    {
    	ArrayList existingInventoryDataList = null;
        String lineItemId                   = null;
        HashMap inventoryDataListMap        = new HashMap();

    	if( dbFinalInventoryDataListMap != null && dbFinalInventoryDataListMap.isEmpty() == false )
    	{
            Iterator it = dbFinalInventoryDataListMap.keySet().iterator();

			while( it.hasNext() )
			{
				lineItemId	= (String) it.next() ;
                existingInventoryDataList = ( ArrayList ) existingFinalInventoryDataListMap.get( lineItemId );

                if( existingInventoryDataList != null && existingInventoryDataList.isEmpty() == false )
			    {
                    //inventoryDataListMap.put( lineItemId , existingInventoryDataList  );
                    existingFinalInventoryDataListMap.put( lineItemId , existingInventoryDataList  );
                }
                else
                {
                    //inventoryDataListMap.put( lineItemId , dbFinalInventoryDataListMap.get( lineItemId )  );
                    existingFinalInventoryDataListMap.put( lineItemId , dbFinalInventoryDataListMap.get( lineItemId )  );
                }
            }
    	}
        else
        {
            inventoryDataListMap = existingFinalInventoryDataListMap;
        }
        return inventoryDataListMap;
    }
    private String getShipAlertMessageBody( String host, PackingSlipForm packingSlipForm)
    {
        StringBuffer htmlView = new StringBuffer();
        String MFG_PART_NUMBER_KEY 	= "";
        String CUSTOMER_PART_NUMBER_KEY = "";
        String CUSTOMER_LABEL_NUMBER_KEY = "";
        String WEIGHT_MEASUREMENT = "";

        try
		{
			 MFG_PART_NUMBER_KEY = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.mfgPartNumber" );
		}
		catch ( NullPointerException e )
		{
			 MFG_PART_NUMBER_KEY = Labels.DEFAULT_MFG_PART_NUMBER_LABEL;
		}

        try
		{
			 CUSTOMER_PART_NUMBER_KEY = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.customerPartNumber" );
		}
		catch ( NullPointerException e )
		{
			 CUSTOMER_PART_NUMBER_KEY = Labels.DEFAULT_CUSTOMER_PART_NUMBER_LABEL;
		}

        try
		{
			 CUSTOMER_LABEL_NUMBER_KEY = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.labelNumber" );
		}
		catch ( NullPointerException e )
		{
			 CUSTOMER_LABEL_NUMBER_KEY = "Label Number";
		}
        try
		{
			 WEIGHT_MEASUREMENT = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.packingSlip.WeightMeasurements" );
		}
		catch ( NullPointerException e )
		{
			 WEIGHT_MEASUREMENT = Labels.DEFAULT_WEIGHT_MEASUREMENT_LABEL;
		}


        String packingSlipNumber = packingSlipForm.getLabel();
        if( ( packingSlipNumber == null ) || ( packingSlipNumber != null && packingSlipNumber.trim().length() == 0 ) ) { packingSlipNumber = "&nbsp;"; }

        String invoiceNumber = packingSlipForm.getInvNo();
        if( ( invoiceNumber == null ) || ( invoiceNumber.trim().length() == 0 ) ) { invoiceNumber = "&nbsp;"; }

        String poNumber = packingSlipForm.getCustomerPO();
        if( ( poNumber == null ) || ( poNumber.trim().length() == 0 ) ) { poNumber = "&nbsp;"; }

        String cartonCount = packingSlipForm.getNoOfBoxes();
        if( ( cartonCount == null ) || ( cartonCount.trim().length() == 0 ) ) { cartonCount = "&nbsp;"; }

        String uomPkgDimension = packingSlipForm.getUomPkgDimension();
        if( ( uomPkgDimension == null ) || ( uomPkgDimension.trim().length() == 0 ) ) { uomPkgDimension = "&nbsp;"; }

        String uomWeight = packingSlipForm.getWeightType();
        if( ( uomWeight == null ) || ( uomWeight.trim().length() == 0 ) ) { uomWeight = "&nbsp;"; }

        String weight = packingSlipForm.getWeight();
        if( ( weight == null ) || ( weight.trim().length() == 0 ) ) { weight = "&nbsp;"; }

        String grossWeight = packingSlipForm.getGrossWeight();
        if( ( grossWeight == null ) || ( grossWeight.trim().length() == 0 ) ) { grossWeight = "&nbsp;"; }

        String packageDimension = packingSlipForm.getPackageDimension();
        if( ( packageDimension == null ) || ( packageDimension.trim().length() == 0 ) ) { packageDimension = "&nbsp;"; }

        String assmCountryOfOrigin = packingSlipForm.getCountryOfAssyOrigin();
        if( ( assmCountryOfOrigin == null ) || ( assmCountryOfOrigin.trim().length() == 0 ) ) { assmCountryOfOrigin = "&nbsp;"; }

        String fabCountryOfOrigin = packingSlipForm.getCountryOfFabOrigin();
        if( ( fabCountryOfOrigin == null ) || ( fabCountryOfOrigin.trim().length() == 0 ) ) { fabCountryOfOrigin = "&nbsp;"; }

        String shippingMethod = packingSlipForm.getShippedVia();
        if( ( shippingMethod == null ) || ( shippingMethod.trim().length() == 0 ) ) { shippingMethod = "&nbsp;"; }

        String deliveryTerms = packingSlipForm.getFob();
        if( ( deliveryTerms == null ) || ( deliveryTerms.trim().length() == 0 ) ) { deliveryTerms = "&nbsp;"; }

        String status = packingSlipForm.getStatus();
        if( ( status == null ) || ( status != null && status.trim().length() == 0 ) ) { status = "&nbsp;"; }

        String noOfPalettes = packingSlipForm.getPaletteCount();
        if( ( noOfPalettes == null ) || ( noOfPalettes != null && noOfPalettes.trim().length() == 0 ) ) { noOfPalettes = "&nbsp;"; }

        String packedby = packingSlipForm.getPackedby();
        if( ( packedby == null ) || ( packedby != null && packedby.trim().length() == 0 ) ) { packedby = "&nbsp;"; }

        String title = packingSlipForm.getTitle();
        if( ( title == null ) || ( title != null && title.trim().length() == 0 ) ) { title = "&nbsp;"; }

        String shippingInstructions = packingSlipForm.getShippingInstructions();
        if( ( shippingInstructions == null ) || ( shippingInstructions != null && shippingInstructions.trim().length() == 0 ) ) { shippingInstructions = "&nbsp;"; }

        String comments = packingSlipForm.getComments();
        if( ( comments == null ) || ( comments != null && comments.trim().length() == 0 ) ) { comments = "&nbsp;"; }

        String actualShipDate = packingSlipForm.getShipDate();
        if( ( actualShipDate == null ) || ( actualShipDate != null && actualShipDate.trim().length() == 0 ) ) { actualShipDate = "&nbsp;"; }

        String freightInstructions = packingSlipForm.getFreightInstructions();
        if( ( freightInstructions == null ) || ( freightInstructions != null && freightInstructions.trim().length() == 0 ) ) { freightInstructions = "&nbsp;"; }

        String letterOfCreditNo = packingSlipForm.getLetterOfCreditNo();
        if( ( letterOfCreditNo == null ) || ( letterOfCreditNo != null && letterOfCreditNo.trim().length() == 0 ) ) { letterOfCreditNo = "&nbsp;"; }

        String mawbNo = packingSlipForm.getMawb();
        if( ( mawbNo == null ) || ( mawbNo != null && mawbNo.trim().length() == 0 ) ) { mawbNo = "&nbsp;"; }

        String hawbNo = packingSlipForm.getHawb();
        if( ( hawbNo == null ) || ( hawbNo != null && hawbNo.trim().length() == 0 ) ) { hawbNo = "&nbsp;"; }

        String flightNoOne = packingSlipForm.getFlightNumberOne();
        if( ( flightNoOne == null ) || ( flightNoOne != null && flightNoOne.trim().length() == 0 ) ) { flightNoOne = "&nbsp;"; }

        String flightNoTwo = packingSlipForm.getFlightNumberTwo();
        if( ( flightNoTwo == null ) || ( flightNoTwo != null && flightNoTwo.trim().length() == 0 ) ) { flightNoTwo = "&nbsp;"; }

        String etd = packingSlipForm.getEtd();
        if( ( etd == null ) || ( etd != null && etd.trim().length() == 0 ) ) { etd = "&nbsp;"; }

        String eta = packingSlipForm.getEta();
        if( ( eta == null ) || ( eta != null && eta.trim().length() == 0 ) ) { eta = "&nbsp;"; }
        

        String[] trackingNo = packingSlipForm.getTracking();
        String vendorNumberLabel = null;
        try
		{
            vendorNumberLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.packingSlip.VendorFace.InvoiceNumber" );
            if ( vendorNumberLabel == null )
            {
                vendorNumberLabel = Labels.DEFAULT_VENDOR_NUMBER_LABEL;
            }
        }
        catch ( NullPointerException e )
		{
			 vendorNumberLabel = Labels.DEFAULT_VENDOR_NUMBER_LABEL;
		}

        htmlView.append( "<HTML><HEAD><TITLE>Scalar Soft - ChipTrac</TITLE><META http-equiv=Content-Type content='text/html; charset=windows-1252'></HEAD><style>.NArialL{ COLOR: #000000;font-family: verdana;font-size: 8pt;font-weight:bold}.NArialRS{COLOR: #000000;FONT-FAMILY: verdana;FONT-SIZE: 7pt;}</style>");
        htmlView.append( "<BODY><table bgcolor='#FFFFF0' border='1' width='800' cellspacing=1 bordercolor='#CCCCFF' align=center valign=top class='NArialL'>" );
        htmlView.append( "<tr><td><table border='0' width='98%' cellspacing=5 bordercolor='#CCCCFF' align=center valign=top class='NArialL'><tr><td colspan=5><br></td></tr><tr height=5>" );
        htmlView.append( "<td><img src='http://" + host + "/" + ConfigManager.getChipTracContext() + "/jsp/includes/images/scalar_logo.jpg' alt='LOGO' height='72' width='180'></td><td colspan=3 align=center><font size=5><b><u>Shipment Notification</u></b></font><br><br></td><td>&nbsp;</td></tr>");
        htmlView.append( "<tr height=2><td bgcolor='#993333' colspan=2>&nbsp;</td><td width='10%'></td><td bgcolor='#993333' colspan=2>&nbsp;</td></tr>" );
        htmlView.append( "<tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;");
        htmlView.append( vendorNumberLabel );
        htmlView.append( ":</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( packingSlipNumber );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;PO Number:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( poNumber );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Actual Ship Date: (mm/dd/yyyy)</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( actualShipDate );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;No. Of Boxes:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( cartonCount );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Shipping Instructions:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( shippingInstructions );
        if( ConfigManager.getShowPackageDimensionInPackingSlip() )
        {
            htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Net Weight:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        }
        else
        {
            htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Weight:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        }
        htmlView.append( weight + "&nbsp;" + uomWeight );
        if( ConfigManager.getShowPackageDimensionInPackingSlip() )
        {
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Package Dimension:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( packageDimension + "&nbsp;" + uomPkgDimension );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Gross Weight:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( grossWeight + "&nbsp;" + uomWeight );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Country of Origin(Fab):</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( fabCountryOfOrigin );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Country of Origin(Assy):</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( assmCountryOfOrigin );
        }
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Packed By:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( packedby );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Forwarder:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( shippingMethod );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Title:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( title );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Delivery Terms:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( deliveryTerms );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Freight Instructions:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( freightInstructions );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Number of Pallets:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( noOfPalettes );
        if( ConfigManager.getShowPackageDimensionInPackingSlip() )
        {
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;MAWB No:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( mawbNo );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;HAWB No:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( hawbNo );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Flight-1 No:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( flightNoOne );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Flight-2 No:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( flightNoTwo );
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;ETD:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( etd );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;ETA:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( eta );
        }
        htmlView.append( "</td></tr><tr><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Freight Tracking No:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append ( "<table class='NArialRS'>" );
        if( trackingNo != null && trackingNo.length > 0 )
        {
            for(int i = 0 ; i < trackingNo.length ; i++ )
            {
                htmlView.append( "<tr><td class='NArialL' >" + trackingNo[i] + "</td></tr>" );
            }
        }
        else
        {
             htmlView.append( "<tr><td>&nbsp;</td></tr>" );
        }
        htmlView.append ( "</table>" );
        htmlView.append( "</td><td width='10%' NOWRAP>&nbsp;</td><td width='15%' align='left' bgcolor='#DEE1E2' bgcolor='#DEE1E2' NOWRAP>&nbsp;&nbsp;Comments:</td><td width='30%' align='left' bgcolor='#DEE1E2' NOWRAP>" );
        htmlView.append( comments );
        htmlView.append( "</td></tr>" );

        htmlView.append ( "<tr><td>&nbsp;</td></tr>" );
        /* LINE ITEM DETAILS*/
        if( packingSlipForm.isDropShippment() == true )
        {
            htmlView.append( "<tr><td colspan=5>" );
            htmlView.append( "<table border='0' width='100%' bordercolor='#FFFFFF'><tr>" );
            htmlView.append( "<td width='4%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Line Item</font></td>" );
            htmlView.append( "<td width='15%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>"+ MFG_PART_NUMBER_KEY +"</font></td>" );
            htmlView.append( "<td width='15%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Shipped "+ MFG_PART_NUMBER_KEY +"</font></td>" );
            htmlView.append( "<td width='15%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>" + CUSTOMER_PART_NUMBER_KEY + "</font></td>" );
            htmlView.append( "<td width='30%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Description</font></td>" );
            htmlView.append( "<td width='15%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Unit Type</font></td>" );
            htmlView.append( "<td width='10%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Req. Dock Date</font></td>" );
            htmlView.append( "<td width='10%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Sch. Ship Date</font></td>" );
            htmlView.append( "<td width='10%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Quantity To be Shipped</font></td>" );
            htmlView.append( "<td width='10%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Quantity Shipped</font></td>" );
            htmlView.append( "<td width='8%' align='center' bgcolor='#993333'><font class='NArialL'><font color='#FFFFFF'>Status</font></td></tr>" );
            CustomerShippingLineItems csLineItem = null;
            ArrayList lineItems = packingSlipForm.getLineitems();
			int lineItemCount 	= lineItems.size();
            String shippedSKU = null;

            for( int i = 0 ; i < lineItemCount ; i++ )
            {

                csLineItem = ( CustomerShippingLineItems ) lineItems.get (i);

                String lineItemNoStr           = "";
                int lineItemNo              = csLineItem.getLineitemNumber();
                if(  lineItemNo == 0 )
                    lineItemNoStr = "&nbsp;";
                else
                    lineItemNoStr = lineItemNo + "";


                String partNumber           = csLineItem.getPart_Number();
                if( ( partNumber == null ) || ( partNumber != null && partNumber.trim().length() == 0 ) ) { partNumber = "&nbsp;"; }

                String customerPartNumber 	= csLineItem.getCustomer_Part_Number();
                if( ( customerPartNumber == null ) || ( customerPartNumber != null && customerPartNumber.trim().length() == 0 ) ) { customerPartNumber = "&nbsp;"; }

                String customerLabelNumber 	= csLineItem.getCustomer_Label_Number();
                if( ( customerLabelNumber == null ) || ( customerLabelNumber != null && customerLabelNumber.trim().length() == 0 ) ) { customerLabelNumber = "&nbsp;"; }

                String description 			= csLineItem.getCool_Description()  ;
                if( ( description == null ) || ( description != null && description.trim().length() == 0 ) ) { description = "&nbsp;"; }

                String units 				= ( csLineItem.getUnits() == null ? "" : csLineItem.getUnits() );
                if( ( units == null ) || ( units != null && units.trim().length() == 0 ) ) { units = "&nbsp;"; }

                String requestedDate        = csLineItem.getCool_Requested_Date();
                if( ( requestedDate == null ) || ( requestedDate != null && requestedDate.trim().length() == 0 ) ) { requestedDate = "&nbsp;"; }

                String scheduleDate         = ( ( csLineItem.getCool_Revised_Date() != null && csLineItem.getCool_Revised_Date().trim().length() > 0 ) ? csLineItem.getCool_Revised_Date() : csLineItem.getCool_Schedule_Date() ) ;
                if( ( scheduleDate == null ) || ( scheduleDate != null && scheduleDate.trim().length() == 0 ) ) { scheduleDate = "&nbsp;"; }


                String qtyToShipStr              = "";
                String qtyPulledStr            = "";
                int    qtyToShip              = csLineItem.getQtyToShip();
                int    qtyPulled            = csLineItem.getQtyPulled();

                if(  qtyToShip == 0 )
                    qtyToShipStr = "&nbsp;";
                else
                    qtyToShipStr = qtyToShip + "";

                if( qtyPulled == 0 )
                    qtyPulledStr = "&nbsp;";
                else
                    qtyPulledStr = qtyPulled + "";

                htmlView.append( "<tr bgcolor='#DEE1E2'>" );
                htmlView.append( "<td width='4%'  align='center' class='NArialL'>" + lineItemNoStr + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + partNumber + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" );
                htmlView.append ("<table border='0' width='100%'>");
                String[] shippedSKUList = csLineItem.getShipped_Part_Number();
                if( shippedSKUList != null )
                {
                    for ( int v = 0; v < shippedSKUList.length; v++ )
                    {
                        shippedSKU = shippedSKUList[v];
                        if( shippedSKU != null && shippedSKU.trim().length() > 0 && ! csLineItem.getPart_Number().equalsIgnoreCase( shippedSKU )  )
                        {
                            htmlView.append ( "<tr><td align='left' valign='middle' class='NArialL'>" + shippedSKU + "</td></tr>");
                        }
                        shippedSKU = "";
                    }
                }
                htmlView.append ( "</table></td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + customerPartNumber + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + description + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + units + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + requestedDate + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + scheduleDate + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + qtyToShipStr + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + qtyPulledStr + "</td>" );
                htmlView.append( "<td width='15%' align='center' class='NArialL'>" + status + "</td>" );
                htmlView.append( "</tr>");
            }
            htmlView.append( "</table></td></tr>" );
        }

        htmlView.append( "<tr><td colspan=5><br><br><br><table border=0 cellspacing=0 cellpadding=0 width=60% align=left>" );
        htmlView.append( "<font size=-2>&nbsp;</font></table></td></tr></table><br></td></tr></table></BODY></HTML>" );

        return htmlView.toString();
    }
}